//
//  KHTabBarViewController.m
//  KissHealth
//
//  Created by Mark on 2017/1/9.
//  Copyright © 2017年 Mark. All rights reserved.
//

#import "KHTabBarViewController.h"
#import "KHHomeVC.h"
#import "KHVipVC.h"
#import "KHSettingVC.h"
#import "KHTabBar.h"
#import "KHNavigationController.h"
#import "KHCommentVC.h"

@interface KHTabBarViewController ()

@property(nonatomic,strong)NSArray *urls;
@property(nonatomic,copy)NSString *loginedUrl;

@end
@implementation KHTabBarViewController

- (instancetype)initWithURLStrings:(NSArray *)urls withLoginedUrl:(NSString *)loginedUrl{
    self = [super init];
    if (self) {
        self.urls = urls;
        self.loginedUrl = loginedUrl;
        [self setUpChild];
    }
    return self;
}
- (instancetype)initWithURLStrings:(NSArray *)urls {
    self = [super init];
    if (self) {
        self.urls = urls;
        [self setUpChild];
    }
    return self;
}


- (void)setUpChild{
    // 1.初始化子控制器
    
    KHHomeVC *home = [[KHHomeVC alloc] initWithURL:self.urls[0]];
    [self addChildVc:home title:@"首页" image:@"tab_home_n" selectedImage:@"tab_home_s"];
    
    KHVipVC *vipVC = [[KHVipVC alloc] initWithURL:self.urls[1]];
    [self addChildVc:vipVC title:@"会员" image:@"tab_user_n" selectedImage:@"tab_user_s"];
    
    KHSettingVC *settingVC = [[KHSettingVC alloc] initWithURL:self.urls[2]];
    [self addChildVc:settingVC title:@"设置" image:@"tab_setting_n" selectedImage:@"tab_setting_s"];


}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tabBar.backgroundColor = [UIColor whiteColor];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    // 处理登录成功后 推送调转的页面
    if (self.loginedUrl == nil || [self.loginedUrl isEqualToString:@""]) {
        return;
    }else{
        KHCommentVC *commentVC = [[KHCommentVC alloc]initWithURL:self.loginedUrl];
        [self.childViewControllers.firstObject.navigationController pushViewController:commentVC animated:YES];
    }
}
/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
    // 设置子控制器的文字
    childVc.title = title; // 同时设置tabbar和navigationBar的文字
    
    // 设置子控制器的图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];

    if (iOS7) {
        childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    } else {
        childVc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
    }
    
    // 设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0 green:183/255.0 blue:283/255.0 alpha:1];
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    // 先给外面传进来的小控制器 包装 一个导航控制器
    KHNavigationController *nav = [[KHNavigationController alloc] initWithRootViewController:childVc];
    nav.navigationBarHidden = YES;
    // 添加为子控制器
    [self addChildViewController:nav];
}


@end
